package com.hpeng.daily.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardScrapAction")
@Scope("prototype")
public class AssetCardScrapAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page=1;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	
	private String msg;
	
	//用于根据权限生成功能按钮
	@Resource
	private Function function;
	@Resource
	private LoginUserMgr loginUserMgr;
	@Resource
	private LoginUserMgr loginUser;
	private String accessRight;	
	private String FN;
	private String userDepName;


	//资产卡片的业务层
	@Resource
	private AssetCardManager assetCardManager;
	
	private AssetCard assetCard;	//资产卡片
	private String acid;	//物品编号
	
	public Pagination<AssetCard> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPage(){
		return page;
	}
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}
	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}
	public void setAcid(String acid) {
		this.acid = acid;
	}
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setFN(String fN) {
		FN = fN;
	}
	public String getFN() {
		return FN;
	}
	public String getUserDepName() {
		return userDepName;
	}
	public void setUserDepName(String userDepName) {
		this.userDepName = userDepName;
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public String getAccessRight() {
		return accessRight;
	}
	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}
	
	//查询所有状态为在用、盘亏确认和报废过程中资产卡片
	public String searchScrap(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUserMgr.getUserDeptName(request);
		String[] hql = {" from AssetCard where acstatus='400' and depname='"+userDepName+"' or acstatus like '6%' and depname='"+userDepName+"'"};
		String order = " order by acid asc ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("searchInner.action?FN=" + FN);
		this.accessRight = function.getAccessRight(session(), FN);
		
		return "success";
	}
	
	//经办录入
	public String updateAssetCardScrapInput(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//内调部门经办录入的只能是本部门状态处于在用的资产
					if("400".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("601");
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("6", "01"));
					}else{
						msg=getText("经办录入的只能是本部门状态处于在用的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("录入失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("录入成功");
		}
		
		return searchScrap();
	}
	
	//经办上报
	public String updateAssetCardScrapReport(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//部门经办上报的只能是本部门状态处于经办已录入的资产
					if("601".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("602");
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("6", "02"));
					}else{
						msg=getText("经办申请的只能是本部门状态处于经办已申请的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("上报失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("上报成功");
		}
		
		return searchScrap();
	}
	
	//领导审核
	public String updateAssetCardScrapDeclaration(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能审核经办已上报的资产(审核只能单个完成，因为需要建议)
				if("602".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("603");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("6", "03"));
				}else{
					msg=getText("只能审核经办已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		
		return searchScrap();
	}
	
	//领导退回
	public String updateAssetCardScrapUntread(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能退回状态处于经办已上报的资产
				if("602".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("601");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("6", "01"));
				}else{
					msg=getText("只能退回状态处于经办已上报的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		
		return searchScrap();
	}
	
	//领导传递(可以批量传递)
	public String updateAssetCardScrapTransfer(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acidAll=request.getParameter("acidAll");
			if(!"".equals(acidAll)){
				String[] dids =acidAll.split(",");
				for(String id : dids){
					assetCard=assetCardManager.findAssetCard(id);
					//领导向物资部传递的只能是本部门状态处于领导已审核的资产
					if("603".equals(assetCard.getAcstatus())){
						assetCard.setAcstatus("604");
						//写入台帐记录
						assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("6", "04"));
					}else{
						msg=getText("领导向物资部传递的只能是本部门状态处于领导已审核的资产");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("传递失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("传递成功");
		}
		
		return searchScrap();
	}
	
	//物资部确认(物质部经办审核)
	public String updateAssetCardScrapSubstanceDeclaration(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能审核部门领导传递过来的已确认的报废资产(审核只能单个完成，因为需要建议)
				if("604".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("605");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("6", "05"));
				}else{
					msg=getText("只能审核部门领导传递过来的已确认报废的资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("确认核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		
		return searchScrap();
	}
	
	//物资部退回(物质部经办退回)
	public String updateAssetCardScrapSubstanceUntread(){
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String acid=request.getParameter("acid");
			String ideaValue=new String(request.getParameter("ideaValue").getBytes("iso8859-1"),"UTF-8");
			if(!"".equals(acid)){
				assetCard=assetCardManager.findAssetCard(acid);
				//判断只能退回部门领导传递过来的已确认的报废资产
				if("604".equals(assetCard.getAcstatus())){
					assetCard.setAcstatus("603");
					assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),ideaValue,function.getStatusDesc("6", "03"));
				}else{
					msg=getText("只能退回部门领导传递过来的已确认的报废资产");
				}
			}
		} catch (Exception rx) {
			msg = getText("退回失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("退回成功");
		}
		
		return searchScrap();
	}
	
	//条件查询
	public String getScrapAssetCardByPageNumberAndValue(){
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUserMgr.getUserDeptName(request);
		String propertyValue=request.getParameter("propertyValue");
		String pageNumberStr=request.getParameter("page");
		if(pageNumberStr!=null){
			page=Integer.parseInt(pageNumberStr);
		}
		String[] hql=new String[1];
		if("acstatus".equals(propertyValue)){
			try{
				String propertyValueBeChoose=request.getParameter("propertyValueBeChoose"); 
				System.out.println("propertyValueBeChoose--->"+propertyValueBeChoose);
				hql[0] = " from AssetCard where acstatus='400' and "+propertyValue+" like '%"+propertyValueBeChoose+"%' and depname='"+userDepName+"' or acstatus like '6%' and "+propertyValue+" like '%"+propertyValueBeChoose+"%' and depname='"+userDepName+"'";
				String order = " order by acid asc ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("getInnerMoveAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&propertyValueBeChoose="+propertyValueBeChoose);
			} catch (Exception rx) {
				msg = getText("条件查询失败，请重新操作");
			}
		}else{
			String inputValue;
			try {
				inputValue = new String(request.getParameter("inputValue").getBytes("iso8859-1"),"UTF-8");
				System.out.println("inputValue--->"+inputValue);
				hql[0] = " from AssetCard where acstatus='500' and "+propertyValue+" like '%"+inputValue+"%' and depname='"+userDepName+"' or acstatus like '6%' and "+propertyValue+" like '%"+inputValue+"%' and depname='"+userDepName+"'";
				String order = " order by acid asc ";
				setPagination(assetCardManager.getPagination(page, hql, order));
				pageLink = pagination.getPageLink("getInnerMoveAssetCardByPageNumberAndValue.action?FN=" + FN+"&propertyValue="+propertyValue+"&inputValue="+inputValue);
			} catch (Exception rx) {
				msg = getText("条件查询失败，请重新操作");
			}
		}
		this.accessRight = function.getAccessRight(session(), FN);
		
		return "success";
	}
	
	//点击类别树上的类别得到该类别下的处于内调状态的资产
	public String queryAssetScrapInTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		this.userDepName=loginUserMgr.getUserDeptName(request);
		String catid=request.getParameter("catid");
		int page=1;
		if(request.getParameter("page")!=null){
			page=Integer.parseInt(request.getParameter("page"));
		}
		String[] hql = { " from AssetCard where acstatus='400' and catid like '"+catid+"%' and depname='"+userDepName+"' or acstatus like '6%' and catid like '"+catid+"%' and depname='"+userDepName+"'" };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryAssetInnerMoveInTree.action?FN=" + FN+"&catid="+catid);
		
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
}
